/********************************************************************************/
/* Projekt  EBike_02                                                             */
/* Autor:   Gerhard Woelfl                                                      */
/* Datum:   29.05.2014                                                          */
/* Changes: 29.05.2014: Initial Version                                         */
/*                                                                              */
/********************************************************************************/

#include <xc.h>
#include "spi.h"


// ****************************************************************************
//
//	Function: void SPIInit(void)
//
// ****************************************************************************
void SPIInit()
{
    // Interrupt Controller Settings
    IFS0bits.SPI1IF = 0;

    SPI1CON1bits.DISSCK = 0;    // Internal SPI clock is enabled
    SPI1CON1bits.DISSDO = 0;    // SDOx pin is controlled by the module
    SPI1CON1bits.MODE16 = 0;    // Communication is byte-wide (8 bits)
    SPI1CON1bits.SMP = 0;       // Input data is sampled at middle of data output time
    SPI1CON1bits.CKE = 0;       // Serial output data changes on transition from active clock state to Idle clock state (refer to bit 6)
    SPI1CON1bits.CKP = 1;       // Idle state for clock is a low level; active state is a high level
    SPI1CON1bits.MSTEN = 1;     // Master mode
    SPI1CON1bits.SPRE = 0b000;  // Secondary Prescale 8:1
    SPI1CON1bits.PPRE = 0b01;   // Priamry Prescale 16:1

    SPI1CON2bits.FRMEN = 0;     // Framed SPIx support disabled
    SPI1CON2bits.SPIBEN = 0;    // Enhanced Buffer is disabled (Legacy mode)


    // SPI1STAT Register Settings
    SPI1STATbits.SPISIDL = 0; // Continue module operation in Idle mode
    SPI1STATbits.SPIBEC = 0; // Buffer Length = 1 Word
    SPI1STATbits.SPIROV = 0; // No Receive Overflow has occurred
    SPI1STATbits.SPIEN = 1; // Enable SPI module

    // Ab hier DMA setup

    /*
    // Force First Word After Enabling SPI
    DMA0REQbits.FORCE=1;
    while (DMA0REQbits.FORCE == 1);
    IEC0bits.SPI1IE = 1;

    // Set up DMA Channel 0 to Transmit in Continuous Ping-Pong Mode:
    IFS0bits.DMA0IF = 0;
    IEC0bits.DMA0IE = 1;
    DMA0CONbits.SIZE = 1;       // Byte
    DMA0CONbits.DIR = 1;        // Reads from RAM address, writes to peripheral address
    DMA0CONbits.MODE = 0b10;    // Continuous, Ping-Pong modes are enabled
    DMA0STAL = (uint16_t)&TxBufferA;
    DMA0STAH = (uint16_t)&TxBufferB;
    DMA0PAD = (volatile uint16_t) &SPI1BUF;
    DMA0CNT = 15;
    DMA0REQ = 0x000A;
    DMA0CONbits.CHEN = 1;

    // Set up DMA Channel 1 to Receive in Continuous Ping-Pong Mode:
    IFS0bits.DMA1IF = 0;
    IEC0bits.DMA1IE = 1;
    DMA1CONbits.SIZE = 1;       // Byte
    DMA1CONbits.DIR = 0;        // Reads from RAM address, writes to peripheral address
    DMA1CONbits.MODE = 0b10;    // Continuous, Ping-Pong modes are enabled
    DMA1STAL = (uint16_t)&RxBufferA;
    DMA1STAH = (uint16_t)&RxBufferB;
    DMA1PAD = (volatile uint16_t) &SPI1BUF;
    DMA1CNT = 15;
    DMA1REQ = 0x000A; // IRQ source is SPI1
    DMA1CONbits.CHEN = 1;
    */

}
